package com.service;

import com.dto.DeptDto;
import org.springframework.jdbc.core.JdbcTemplate;
import com.rowmapper.DeptRowMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional
public class DeptService {

    private final JdbcTemplate jdbcTemplate;

    public DeptService(JdbcTemplate jdbcTemplate) {

        this.jdbcTemplate = jdbcTemplate;
    }

    public void addDept(DeptDto deptDto){

        if(deptDto.getId()<0){
            throw new RuntimeException("deptId = "+deptDto.getId()+" 不能小于 0 ");
        }

        jdbcTemplate.execute("insert into dept(`name`) values('"+deptDto.getName()+"')");
    }

    public List<DeptDto> page(){

        return jdbcTemplate.query("select * from dept",new DeptRowMapper());
    }

}
